const Sequelize = require('sequelize')
let db = require('../config/db')
let User = require('./user')

let Session = db.define('session',{
    id:{
        type: Sequelize.INTEGER,
        field: 'id',
        primaryKey:true,
        autoIncrement: true,
        comment: "主键，自增"
    },
    uid:{
        type: Sequelize.INTEGER
    },
    openid:{
        type:Sequelize.STRING
    },
    access_token:{
        type:Sequelize.STRING,
        comment:'access_token'
    },
    exp_time:{
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('exp_time')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    isDel:{
        type: Sequelize.INTEGER,
        defaultValue:0,
        comment:'状态，是否删除'
    },
    createdAt: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    },
    updatedAt: {
        type: Sequelize.DATE,
        get() {
            return moment(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss');
        }
    }
},{
    freezeTableName:true,
    paranoid:true,
    timestamps:true
})
Session.belongsTo(User, {foreignKey: 'uid', targetKey: 'uid'});
let session = Session.sync();
module.exports = Session
